package gs.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class ConnectionWindow extends JFrame implements ActionListener {
	
	JTextField ipField;
	JTextField portField;
	JTextField userField;
	JPasswordField passwordField;
	JButton quitButton;
	JButton connectButton;
	JLabel infoLabel;
	ConnectionController controller;
	AdminWindow aframe;
	
	
	public void initUI() {
		this.setLayout(new BorderLayout());
		
		setTitle("Opcje po��czenia");
		setSize(300, 150);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);     
		
		JPanel pane = new JPanel(new GridLayout(5,2));
		
		infoLabel = new JLabel("Wpisz parametry po��czenia");
		
		quitButton = new JButton("Wyj�cie");
		quitButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
		        System.exit(0);
		    }
		});
		
		connectButton = new JButton("Po��cz");
		connectButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
		        connect();
		    }
		});
		
		ipField = new JTextField("127.0.0.1");
		portField = new JTextField("3306");
		userField = new JTextField("root");
		passwordField = new JPasswordField("laborkiczlowieku");
		
		pane.add(new JLabel("Adres IP:"));
		pane.add(ipField);
		
		pane.add(new JLabel("Port:"));
		pane.add(portField);
		
		pane.add(new JLabel("U�ytkownik:"));
		pane.add(userField);
		
		pane.add(new JLabel("Has�o:"));
		pane.add(passwordField);
		
		pane.add(quitButton);
		pane.add(connectButton);
		
		getContentPane().add(pane,BorderLayout.CENTER);
		getContentPane().add(infoLabel, BorderLayout.SOUTH);
		
	}
	
	public ConnectionWindow(ConnectionController gs, AdminWindow a) {
        initUI();  
        controller = gs;
        aframe = a;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void connect()
	{
		if(controller.connect(ipField.getText(), portField.getText(), userField.getText(), String.valueOf(passwordField.getPassword())))
		{
			infoLabel.setText("Po��czenie nawiazane");
			aframe.setVisible(true);
			aframe.initUI();
			this.setVisible(false);
		}
		else
		{
			infoLabel.setText("B��d po��czenia");
		}
	}
}
